package com.csii.ldap.controller;

import com.csii.ldap.api.Result;
import com.csii.ldap.dto.UserDTO;
import com.csii.ldap.entity.Group;
import com.csii.ldap.entity.GroupUser;
import com.csii.ldap.service.GroupService;
import com.csii.ldap.service.GroupUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ldap.query.LdapQuery;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

import static org.springframework.ldap.query.LdapQueryBuilder.query;

/**
 * @author danjiawei
 * @desc
 * @date 2020/9/4
 */
@RequestMapping("user")
@RestController
@Api(value = "ldap操作用户")
public class GroupUserController {

    @Autowired
    private GroupUserService groupUserService;
    @Autowired
    private GroupService groupService;

    @PostMapping("add")
    @ApiOperation(value = "添加用户")
    public Result<GroupUser> add(@RequestBody UserDTO dto) {
        // 创建groupUser对象
        GroupUser groupUser = new GroupUser();
        BeanUtils.copyProperties(dto, groupUser);
        groupUser.setUid(dto.getFullName());
        groupUser.setUidNumber(1000);
        groupUser.setHomeDirectory("/home/users/" + dto.getEmail());

        // 根据组名查询组的gidNumber
        Optional<Group> group = groupService.findOne(getQuery(dto.getGroupName()));
        if (!group.isPresent()) {
            return Result.error("组名不存在");
        }
        groupUser.setGidNumber(group.get().getGidNumber());
        GroupUser save = groupUserService.save(groupUser);
        // 将用户添加进组
        Group group1 = group.get();
        group1.getMemberUid().add(dto.getFullName());
        groupService.save(group1);
        return Result.ok(save);
    }

    @PostMapping("changePassword")
    @ApiOperation(value = "修改密码")
    public Result changePassword(@RequestBody UserDTO dto) {
        // 根据邮箱查询用户信息
        Optional<GroupUser> groupUser = groupUserService.findOne(getQuery(dto.getFullName()));
        if (!groupUser.isPresent()) {
            return Result.error("用户不存在");
        }
        GroupUser user = groupUser.get();
        user.setPassword(dto.getPassword());
        groupUserService.save(user);
        return Result.ok("密码修改成功");
    }

    @PostMapping("deleteUser")
    @ApiOperation(value = "删除用户")
    public Result deleteUser(@RequestBody UserDTO dto) {
        // 根据邮箱查询用户信息
        Optional<GroupUser> groupUser = groupUserService.findOne(getQuery(dto.getFullName()));
        if (!groupUser.isPresent()) {
            return Result.error("用户不存在");
        }
        groupUserService.delete(groupUser.get());
        return Result.ok("删除成功");
    }

    @PostMapping("addUserToGroup")
    @ApiOperation(value = "将用户添加到小组")
    public Result addUserToGroup(@RequestBody UserDTO dto) {
        // 根据邮箱查询用户信息
        Optional<GroupUser> groupUser = groupUserService.findOne(getQuery(dto.getFullName()));
        if (!groupUser.isPresent()) {
            return Result.error("用户不存在");
        }
        // 根据组名查询组信息
        Optional<Group> group = groupService.findOne(getQuery(dto.getGroupName()));
        if (!group.isPresent()) {
            return Result.error("组名不存在");
        }
        Group group1 = group.get();
        group1.getMemberUid().add(groupUser.get().getUid());
        groupService.save(group1);
        return Result.ok("添加成功");
    }

    @PostMapping("rmUserForGroup")
    @ApiOperation(value = "将用户移除小组")
    public Result rmUserForGroup(@RequestBody UserDTO dto) {
        // 根据邮箱查询用户信息
        Optional<GroupUser> groupUser = groupUserService.findOne(getQuery(dto.getFullName()));
        if (!groupUser.isPresent()) {
            return Result.error("用户不存在");
        }
        // 根据组名查询组信息
        Optional<Group> group = groupService.findOne(getQuery(dto.getGroupName()));
        if (!group.isPresent()) {
            return Result.error("组名不存在");
        }
        Group group1 = group.get();
        group1.getMemberUid().remove(groupUser.get().getUid());
        groupService.save(group1);
        return Result.ok("移除成功");
    }


    public static LdapQuery getQuery(String name) {
        return query().where("cn").is(name);
    }

}
